package com.wangxiang.shop.product.service.impl;

import com.wangxiang.common.utils.MyStringUtile;
import com.wangxiang.shop.product.dao.SpuDao;
import com.wangxiang.shop.product.entity.SpuEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangxiang.common.utils.PageUtils;
import com.wangxiang.common.utils.Query;

import com.wangxiang.shop.product.dao.SkuDao;
import com.wangxiang.shop.product.entity.SkuEntity;
import com.wangxiang.shop.product.service.SkuService;


@Service("skuService")
public class SkuServiceImpl extends ServiceImpl<SkuDao, SkuEntity> implements SkuService {
    @Autowired
    private SkuDao skuDao;
    @Autowired
    private SpuDao spuDao;

    @Override
    public List<SkuEntity> getSkuList(List<Integer> skuIdList) {
        ArrayList<SkuEntity> skuEntities = new ArrayList<>();
//        System.out.println("服务层数据id"+skuIdList);
        skuIdList.forEach((item)->{
            SkuEntity skuEntity = skuDao.selectById(item);
//            System.out.println("skuidEn"+skuEntity);
            skuEntities.add(skuEntity);
        });
//        System.out.println(skuEntities);
        return skuEntities;
    }

    @Override
    public void updateWare(List<HashMap<String, Integer>> maps) {
        for (HashMap<String, Integer> map : maps) {
            Integer skuId = map.get("skuId");
            Integer num = map.get("num");
            SkuEntity skuEntity = skuDao.selectById(skuId);
            int i = skuDao.updateWare(skuId, skuEntity.getWare()-num);
            if (i>0){
                SpuEntity spuEntity = spuDao.selectById(skuEntity.getSpuId());
                spuDao.updateWare(skuEntity.getSpuId(),spuEntity.getWareTotal()-num);
            }


        }
    }

    @Override
    public List<HashMap<String, String>> order(List<HashMap<String, Integer>> list) {
       List<HashMap<String, String>> hashMaps = new ArrayList<>();
        for (HashMap<String, Integer> map : list) {
            HashMap<String, String> orderMap = new HashMap<>();

            //查spu
            SpuEntity spuEntity = spuDao.selectById(map.get("spuId"));
            SkuEntity skuEntity = skuDao.selectById(map.get("skuId"));
            orderMap.put("orderId",String.valueOf(map.get("orderId")));
            //店铺
            orderMap.put("store",spuEntity.getStore());
            //image
            orderMap.put("image",spuEntity.getImage());
            //名称
            orderMap.put("name",spuEntity.getName());
            //样式
            orderMap.put("style",skuEntity.getStyle());
            //型号
            orderMap.put("size",skuEntity.getSize());

            hashMaps.add(orderMap);

        }

        return hashMaps;
    }

    @Override
    public int paynow(HashMap<String,String> map) {

        MyStringUtile myStringUtile = new MyStringUtile();
        ArrayList<Integer> skuIdList = myStringUtile.getSkuArray(map.get("skuIdList"));
//        System.out.println("全部id"+skuIdList);
        ArrayList<SkuEntity> skuEntities = new ArrayList<>();

        ArrayList<SkuEntity> list = new ArrayList<>();
        for (Integer integer : skuIdList) {
            SkuEntity skuEntity = skuDao.selectById(integer);
//            System.out.println("查询结果便利"+skuEntity);
            //没有样式的情况
            skuEntities.add(skuEntity);

        }
        //styel null size true
        if (map.get("style")==null&map.get("size")!=null){
//            System.out.println("满足一");
            for (SkuEntity skuEntity : skuEntities) {
                if (skuEntity.getStyle()==null&map.get("size").equals(skuEntity.getSize())){
                    list.add(skuEntity);
                }
            }
        }
        if (map.get("style")!=null&map.get("size")==null){
//            System.out.println("满足二");
            for (SkuEntity skuEntity : skuEntities) {
                if (map.get("style").equals(skuEntity.getStyle())&skuEntity.getSize()==null){
                    list.add(skuEntity);
                }
            }
        }
        if (map.get("style")!=null&map.get("size")!=null){
//            System.out.println("满足三");
            for (SkuEntity skuEntity : skuEntities) {
                if (map.get("style").equals(skuEntity.getStyle())&map.get("size").equals(skuEntity.getSize())){
                    list.add(skuEntity);
                }
            }
        }



        return list.get(0).getId();
    }

    @Override
    public int updateWareByPayNow(HashMap<String , Integer> map) {
        int skuId = map.get("skuId");
        int num = map.get("num");
        SkuEntity skuEntity = skuDao.selectById(skuId);
        int i = skuDao.updateWare(skuId, skuEntity.getWare() - num);
        if (i>0){
            SpuEntity spuEntity = spuDao.selectById(skuEntity.getSpuId());
            int i1 = spuDao.updateWare(spuEntity.getId(), spuEntity.getWareTotal() - num);
            return i1;
        }


        return 0;
    }
}